package com.tuanzi.loan.business.repository.galaxycard.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardPrivilege.LOAN_GALAXY_CARD_PRIVILEGE;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardRefPrivilege.LOAN_GALAXY_CARD_REF_PRIVILEGE;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanGalaxyCardType.LOAN_GALAXY_CARD_TYPE;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.galaxycard.GalaxyCardPrivilegeRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.galaxycard.GalaxyCardPrivilegeSearchRequest;
import com.tuanzi.loan.vo.response.galaxycard.GalaxyCardPrivilegeSearchResponse;

@Repository
public class GalaxyCardPrivilegeRepositoryImpl extends JOOQBaseRepositoryPlus implements GalaxyCardPrivilegeRepository {

    public enum PrivilegeType{
        TYPE_0("贷款VIP特权"),
        TYPE_1("金融VIP特权"),
        TYPE_2("娱行VIP特权");

        private String name;
        PrivilegeType(String name){
            this.name = name;
        }
        public static String get(Integer index){
            if (Objects.nonNull(index) && index < values().length && index >= 0)
                return values()[index].name;
            return "";
        }
        public static Map<String,Integer> toMap(){
            Map<String,Integer> map = new HashMap<>();
            for (PrivilegeType privilegeType : values())
                map.put(privilegeType.name, privilegeType.ordinal());
            return map;
        }
    }

    public enum PrivilegeCode{
        DEFAULT_BUSINESS("default", "跳转链接类型权益"),
        DEFAULT_EXPRESS("express", "基本快递类型权益"),
        RECHARGE_PHONE_FEE("recharge-phone-fee", "话费充值类型权益"),
        BUSINESS_APPLY("business-apply", "业务申请类型权益"),
        ACTIVATION_CODE("activation-code", "激活码类型权益");
        private String code;
        private String name;
        PrivilegeCode(String code, String name){
            this.code = code;
            this.name = name;
        }
        public String getCode(){
            return code;
        }
        public static String getName(String code){
            for (PrivilegeCode privilegeCode : values()){
                if (privilegeCode.code.equals(code))
                    return privilegeCode.name;
            }
            return "";
        }
        public static Map<String,String> toMap(){
            Map<String,String> map = new HashMap<>();
            for (PrivilegeCode privilegeCode : values())
                map.put(privilegeCode.name, privilegeCode.code);
            return map;
        }
    }

    @Override
    public Map<String, Long> getCardType(){
        return dslContext.select(LOAN_GALAXY_CARD_TYPE.NAME, LOAN_GALAXY_CARD_TYPE.ID)
                .from(LOAN_GALAXY_CARD_TYPE)
                .fetchMap(LOAN_GALAXY_CARD_TYPE.NAME, LOAN_GALAXY_CARD_TYPE.ID);
    }

    @Override
    public Integer countPrivilegeByType(Integer type){
        return dslContext.fetchCount(
                dslContext.select(LOAN_GALAXY_CARD_PRIVILEGE.ID)
                        .from(LOAN_GALAXY_CARD_PRIVILEGE)
                        .where(LOAN_GALAXY_CARD_PRIVILEGE.PRIVILEGE_TYPE.eq(type))
        );
    }

    /**
     * GalaxyCardPrivilege查询
     *
     * @param searchRequest 请求信息
     * @param pageable      分页信息
     * @return 响应列表
     */
    @Override
    public Page<GalaxyCardPrivilegeSearchResponse> list(GalaxyCardPrivilegeSearchRequest searchRequest, Pageable pageable) {
    		
    		fields(LOAN_GALAXY_CARD_PRIVILEGE.fields());
    		fields(LOAN_GALAXY_CARD_REF_PRIVILEGE.PRIVILEGE_ID);
    		leftJoin(LOAN_GALAXY_CARD_REF_PRIVILEGE, LOAN_GALAXY_CARD_PRIVILEGE.ID.eq(LOAN_GALAXY_CARD_REF_PRIVILEGE.PRIVILEGE_ID));
    		contains(LOAN_GALAXY_CARD_PRIVILEGE.NAME, searchRequest.getPrivilegeName());
    		eq(LOAN_GALAXY_CARD_PRIVILEGE.CODE, searchRequest.getPrivilegeCode());
    		eq(LOAN_GALAXY_CARD_PRIVILEGE.PRIVILEGE_TYPE, searchRequest.getPrivilegeType());
    		orderBy(LOAN_GALAXY_CARD_PRIVILEGE.PRIVILEGE_TYPE, Sort.Direction.ASC);
    		return search(pageable, record ->{
    			GalaxyCardPrivilegeSearchResponse response = new GalaxyCardPrivilegeSearchResponse();
              response.setId(record.get(LOAN_GALAXY_CARD_PRIVILEGE.ID));
              response.setPk(record.get(LOAN_GALAXY_CARD_PRIVILEGE.ID));
              response.setCode(PrivilegeCode.getName(record.get(LOAN_GALAXY_CARD_PRIVILEGE.CODE)));
              response.setCreateTime(record.get(LOAN_GALAXY_CARD_PRIVILEGE.CREATE_TIME));
              response.setIconUrl(record.get(LOAN_GALAXY_CARD_PRIVILEGE.ICON_URL));
              response.setName(record.get(LOAN_GALAXY_CARD_PRIVILEGE.NAME));
              response.setSort(record.get(LOAN_GALAXY_CARD_PRIVILEGE.SORT));
              response.setType(PrivilegeType.get(record.get(LOAN_GALAXY_CARD_PRIVILEGE.PRIVILEGE_TYPE)));
              response.setStatus(record.get(LOAN_GALAXY_CARD_REF_PRIVILEGE.PRIVILEGE_ID) != null ? 1 : 0);
              return response;
    		});    		
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_GALAXY_CARD_PRIVILEGE;
    }
}
